package com.gx.smart.lib.http.api;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppDeviceBindRequest;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppDeviceUnBindRequest;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppGetAllRoomsInfoRequest;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppGetAllRoomsInfoResponse;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppRoomInfoProviderGrpc;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppRoomInfoResponse;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionGetRoomCountRequest;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionGetRoomCountResponse;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author xiaosy
 * @create 2020/8/13
 * @Describe
 **/
public class ConstructionAppRoomInfoProviderService {
    private String host = BuildConfig.JOYLIFE_GRPC_SERIVER_URL;
    private String port = "46604";

    private ConstructionAppRoomInfoProviderService() {

    }

    public static ConstructionAppRoomInfoProviderService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private ConstructionAppRoomInfoProviderGrpc.ConstructionAppRoomInfoProviderBlockingStub getStub(ManagedChannel channel) {
        return ConstructionAppRoomInfoProviderGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 绑定网关
     * //房间ID(必)
     * string room_id = 1;
     * //设备序列号(必)
     * string sn = 2;
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, ConstructionAppRoomInfoResponse> deviceBind(
            final String roomId,
            final String sn,
            CallBack<ConstructionAppRoomInfoResponse> callBack) {
        return new GrpcAsyncTask<String, Void, ConstructionAppRoomInfoResponse>(callBack) {
            @Override
            protected ConstructionAppRoomInfoResponse doRequestData(ManagedChannel channel) {
                ConstructionAppDeviceBindRequest message = ConstructionAppDeviceBindRequest.newBuilder()
                        .setRoomId(roomId)
                        .setSn(sn)
                        .build();
                Logger.d(message);
                ConstructionAppRoomInfoResponse response = null;
                try {
                    response = getStub(channel).deviceBind(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "deviceBind");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 删除网关
     * //房间ID(必)
     * string room_id = 1;
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, ConstructionAppRoomInfoResponse> deviceUnbind(
            final String roomId,
            CallBack callBack) {

        return new GrpcAsyncTask<String, Void, ConstructionAppRoomInfoResponse>(callBack) {
            @Override
            protected ConstructionAppRoomInfoResponse doRequestData(ManagedChannel channel) {
                ConstructionAppDeviceUnBindRequest message = ConstructionAppDeviceUnBindRequest.newBuilder()
                        .setRoomId(roomId)
                        .build();
                Logger.d(message);
                ConstructionAppRoomInfoResponse response = null;
                try {
                    response = getStub(channel).deviceUnbind(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "deviceUnbind");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 根据房间ID获取网关信息
     * //房间ID(必)
     * string room_id = 1;
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, ConstructionAppRoomInfoResponse> getSnByRoomId(
            final String roomId,
            CallBack callBack) {
        return new GrpcAsyncTask<String, Void, ConstructionAppRoomInfoResponse>(callBack) {
            @Override
            protected ConstructionAppRoomInfoResponse doRequestData(ManagedChannel channel) {
                ConstructionAppDeviceUnBindRequest message = ConstructionAppDeviceUnBindRequest.newBuilder()
                        .setRoomId(roomId)
                        .build();
                Logger.d(message);
                ConstructionAppRoomInfoResponse response = null;
                try {
                    response = getStub(channel).getSnByRoomId(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "getSnByRoomId");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 获取所有房间信息
     * ws_grpc_com.ComQuery comQuery = 1; //公共分页查询体
     * int32 sn_is_null = 2;//1:网关为空；2：网关不为空
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, ConstructionAppGetAllRoomsInfoResponse> getAllRoomsInfo(
            final ComQuery comQuery,
            final int sn_is_null,
            CallBack<ConstructionAppGetAllRoomsInfoResponse> callBack) {
        return new GrpcAsyncTask<String, Void, ConstructionAppGetAllRoomsInfoResponse>(callBack) {
            @Override
            protected ConstructionAppGetAllRoomsInfoResponse doRequestData(ManagedChannel channel) {
                ConstructionAppGetAllRoomsInfoRequest message = ConstructionAppGetAllRoomsInfoRequest.newBuilder()
                        .setComQuery(comQuery)
                        .setSnIsNull(sn_is_null)
                        .build();
                Logger.d(message);
                ConstructionAppGetAllRoomsInfoResponse response = null;
                try {
                    response = getStub(channel).getAllRoomsInfo(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "getAllRoomsInfo");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 获取所有房间数，和已经绑定网关的房间数
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, ConstructionGetRoomCountResponse> getRoomCount(CallBack<ConstructionGetRoomCountResponse> callBack) {
        return new GrpcAsyncTask<String, Void, ConstructionGetRoomCountResponse>(callBack) {
            @Override
            protected ConstructionGetRoomCountResponse doRequestData(ManagedChannel channel) {
                ConstructionGetRoomCountRequest message = ConstructionGetRoomCountRequest.newBuilder()
                        .build();
                Logger.d(message);
                ConstructionGetRoomCountResponse response = null;
                try {
                    response = getStub(channel).getRoomCount(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "getRoomCount");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    private static class SingleTonHolder {
        private static final ConstructionAppRoomInfoProviderService INSTANCE = new ConstructionAppRoomInfoProviderService();
    }
}
